//
//  ZZLCLoginVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/14.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZLCLoginVC.h"
#import "ZZLCLoginCell.h"
#import "ModifyAccoutSafetyVC.h"
#import "ZZLCRegisterVC.h"
#import "ForgetFirstVC.h"
#import "UserModel.h"
#import "ZZBaseNavigationController.h"
@interface ZZLCLoginVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation ZZLCLoginVC

- (void)viewDidLoad {
    [UserModel clearData];
    [super viewDidLoad];
    self.title = @"登录";
//    [self.tableView setTableHeaderView:[[NSBundle mainBundle]loadNibNamed:@"LoginTopView" owner:nil options:nil][0]];
//    self.tableView.tableHeaderView = [self headView];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self configureNav];
    // Do any additional setup after loading the view from its nib.
}

- (UIView *)headView{
    UIView *headView = [[NSBundle mainBundle]loadNibNamed:@"LoginTopView" owner:nil options:nil].firstObject;
    headView.frame = CGRectMake(0, 0, KWidth, 100);
    return headView;
}
- (void)configureNav
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:UIImageMake(@"logBack") style:UIBarButtonItemStyleDone target:self action:@selector(leftVCShow)];

    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)leftVCShow
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self headView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
          K_WS(weakSelf);
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ZZLCLoginCell" owner:self options:nil];
    ZZLCLoginCell *cell = [nibs firstObject];

    cell.inputPhoneTF.text = @"13951219132";
    cell.inputPwdTF.text = @"123456@";

//    cell.inputPhoneTF.text = @"15167417003";
//    cell.inputPwdTF.text = @"qazwsx";

//        cell.inputPhoneTF.text = @"18516773736";
//        cell.inputPwdTF.text = @"123456";

    
    cell.selectionStyle = NO;
    [cell.loginBtn bk_whenTapped:^{

        [self loadDataByLoginCell :cell];
    }];
    [cell.registerBtn bk_whenTapped:^{
        ZZLCRegisterVC *registerVC = [[ZZLCRegisterVC alloc]initWithNibName:@"ZZLCRegisterVC" bundle:nil];
        registerVC.getValidationCode = GetValidationCodeFormRegister;
        [weakSelf.navigationController pushViewController:registerVC animated:YES];
    }];
    [cell.forgetPWDLabel bk_whenTapped:^{
        ForgetFirstVC *forgetVC = [[ForgetFirstVC alloc]initWithNibName:@"ForgetFirstVC" bundle:nil];
         [weakSelf.navigationController pushViewController:forgetVC animated:YES];
        
    }];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 322;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadDataByLoginCell:(ZZLCLoginCell *)cell{
//    cell.inputPhoneTF.text = @"15167417001";
//    cell.inputPwdTF.text = @"123456";
    [NetWorkingManager postWithRequestUrl:@"login" AndParameters:@{@"parameters":[@{@"username":cell.inputPhoneTF.text,@"password":cell.inputPwdTF.text} DataTOjsonStringAndAddVersionOnlyForLogin]} AndRequestBlock:^(NSDictionary *responseObject) {
        [self configUIByData:responseObject];
    } FailBlock:^(NSDictionary *errDic) {
        NSLog(@"%@",errDic);
    }];
}
- (void)configUIByData:(NSDictionary *)responseObject{
    if ([responseObject[@"end"] isEqualToString:@"ok"]) {
        [[NSUserDefaults standardUserDefaults]setObject:[NSJSONSerialization dataWithJSONObject:[responseObject objectForKey:@"obj"] options:NSJSONWritingPrettyPrinted error:nil]forKey:K_USER_MODEL];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:K_ISLOGIN_SUCCESS];
        [[NSNotificationCenter defaultCenter]postNotificationName:K_NOTIFICATION_ISLOGIN_SUCCESS object:nil];
       [self dismissViewControllerAnimated:YES completion:nil];
    }
    else{
//        NSLog(@"账号或者密码错误");
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
